home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2186 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A Very Simple Socket Question
  5. Date: 19 Jan 1996 19:02:11 GMT
  6. Organization: OpenVision
  7. Message-ID: <4doprj$9t8@spanky.pls.ov.com>
  8. References: <4dfgj8$6p9@nntp.ucs.ubc.ca>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 6p9@nntp.ucs.ubc.ca, evil@unixg.ubc.ca (Peter Pan) writes:
  13. >Please help! I am a C beginner.
  14. >
  15. >======================================
  16. >Client:
  17. >
  18. >int sd, addrlen;
  19. >struct sockaddr_in svaddr;
  20. >
  21. >char *data;
  22. >data = "abcdef";
  23. >
  24. >..... /* skip */
  25. >
  26. >sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
  27.                   ^^^^^^^^^^^^
  28. This will not send the terminating null character, use "strlen(data) + 1".
  29.  
  30. >
  31. >
  32. >==========================================
  33. >Server:
  34. >
  35. >char data;
  36. >data = (char*) malloc( 100*sizeof(char) );
  37. >
  38. >recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
  39.                      ^^^^^^^^^^^^
  40. data is a char * with a size of 4.  What you really want is 100.
  41. Even so, I expect you will block waiting for the remaining 93 chars.
  42. When you use these functions you should agree in advance about
  43. the transfer length, even if you have to precede the actual data
  44. with a message (of known length) containing information about the
  45. length of the data to follow.
  46.  
  47.             Fletcher.Glenn@ov.com
  48.  
  49. >
  50. >==========================================
  51. >
  52. >Output:
  53. >
  54. >data sent: abcdef
  55. >data received: abcd
  56. >
  57. >==========================================
  58. >
  59. >Anyone knows why the data are different after transmittion.
  60. >
  61. >Thanks.
  62. >Leo
  63. >
  64.  
  65.